package com.westos.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class GetCode {
    public static String getCodeByNum(File file, int num) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = null;
        String result = "";
        int num1 = 0;
        int before = num - 5;//当前行的前5行
        int end = num + 5;//当前行的后5行
        //判断给定行号是否小于5行
        if (num > 5) {
            while ((line = bufferedReader.readLine()) != null) {
                num1++;
                if (num1 >= before && num1 <= end) {
                    result += line;
                }else if (num1>end){
                    break;
                }
            }
        } else {
            while ((line = bufferedReader.readLine()) != null) {
                num1++;
                result += line;
                if (num1 > end) {
                    break;
                }
            }
        }
        return result;
    }
}
